from sklearn.linear_model import LinearRegression
import pandas as pd
import numpy as np

df = pd.read_csv('../static/data/info_pre.csv')
df.drop(df[df['total_price_mean'] == 0].index,inplace=True)
df.drop(df[df['area_mean'] == 0].index,inplace=True)
df_scatter = df[['total_price_mean', 'area_mean']]

df_scatter = df_scatter.to_numpy()
data = np.array(df_scatter)
X = data[:, 0].reshape(-1, 1)
y = data[:, 1].reshape(-1, 1)

reg = LinearRegression().fit(X, y)
slope = reg.coef_[0]
intercept = reg.intercept_

X_fit = np.linspace(X.min(), X.max(), 100)
y_fit = slope * X_fit + intercept
df_regression_line = pd.DataFrame({'X_fit': X_fit, 'y_fit': y_fit})
df_regression_line.to_csv('../static/data/line.csv', index=False)